<template>
  <div>
    <div style="margin-bottom: 20px">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/' }" style="margin-left: 20px">系统管理</el-breadcrumb-item>
        <el-breadcrumb-item><a href="/">菜单管理</a></el-breadcrumb-item>
      </el-breadcrumb>
    </div>

    <div style="margin: 10px 0">
      <el-input style="width: 200px" placeholder="请输入名称..." suffix-icon="el-icon-search" class="ml-5"
                v-model="name"></el-input>
      <el-button class="ml-5" type="primary" @click="seachKehu">搜索</el-button>
      <el-button @click="resetForm">重置</el-button>
    </div>
    <div style="margin: 10px 0">
      <el-button type="primary" @click="openKehudialog">新增<i class="el-icon-circle-plus-outline" style="margin-left: 2px"></i></el-button>
      <el-button type="danger" @click="delbath">批量删除<i class="el-icon-remove-outline" style="margin-left: 2px"></i></el-button>
    </div>

    <el-table :data="tableData" border stripe :header-cell-class-name="headerBg"
              row-key="id" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" label="序号" width="50" fixed></el-table-column>
      <el-table-column prop="name" label="名称" width="150px">
      </el-table-column>
      <el-table-column prop="path" label="路径" width="110px">
      </el-table-column>
      <el-table-column prop="pagePath" label="页面路径" width="100px">
      </el-table-column>
      <el-table-column label="图标" width="90px" align="center">
        <template slot-scope="scope">
         <i :class="scope.row.icon" style="font-size: 20px"></i>
        </template>
      </el-table-column>
      <el-table-column prop="description" label="描述" width="280px" >
      </el-table-column>
      <el-table-column label="操作"  fixed="right">
        <template slot-scope="scope">
          <el-button type="info" slot="reference" @click="addMenu(scope.row.id)" v-if="scope.row.pid==0 && !scope.row.path"><i class="el-icon-remove-menu"></i>新增子菜单</el-button>
          <el-button type="primary"  @click="updatekehu(scope.row)"><i class="el-icon-edit"></i>编辑</el-button>
          <el-popconfirm
              class="ml-5"
              confirm-button-text='确定'
              cancel-button-text='取消'
              icon="el-icon-info"
              icon-color="red"
              title="确定要删除这条记录吗？"
              @confirm="deletekehu(scope.row.id)"
          >
            <el-button type="danger" slot="reference"><i class="el-icon-remove-outline"></i>删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="权限信息" :visible.sync="dialogFormVisible" width="30%">
      <el-form :model="form" label-position="left">
        <el-form-item label="名称" label-width="100px">
          <el-col :span="16">
            <el-input v-model="form.name" autocomplete="off" placeholder="请输入名称"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="路径" label-width="100px">
          <el-col :span="16">
            <el-input v-model="form.path" autocomplete="off" placeholder="请输入路径名称"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="页面路径" label-width="100px">
          <el-col :span="16">
            <el-input v-model="form.pagePath" autocomplete="off" placeholder="请输入页面路径"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="图标" label-width="100px">
          <el-col :span="16">
            <template>
              <el-select v-model="form.icon" placeholder="请选择">
                <el-option
                    v-for="item in options"
                    :key="item.name"
                    :label="item.name"
                    :value="item.value">
                  <i :class="item.value">{{item.name}}</i>
                </el-option>
              </el-select>
            </template>
          </el-col>
        </el-form-item>
        <el-form-item label="描述" label-width="100px">
          <el-col :span="16">
            <el-input v-model="form.description" autocomplete="off" placeholder="请输入描述" type="textarea"
                      :autosize="{ minRows: 2, maxRows: 4}"></el-input>
          </el-col>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveKehu">确 定</el-button>
      </div>
    </el-dialog>



  </div>
</template>

<script>
import request from "@/utils/request";
export default {
  name: "Menu",
  data(){
    return {
      tableData: [],
      name: "",
      form:{
        id:"",
        name:"",
        path:"",
        icon:"",
        description:"",
        pid:""
      },
      dialogFormVisible: false,
      imageUrl: '',
      headerBg: 'headerBg',
      multipleSelection: [],
      options:[]
    }
  },
  created() {
    this.load()
  },
  methods: {
    getIcon(){
      request.get("menu/icons").then(res => {
        if (res.code=="200"){
          this.options = res.data
        }
      })
    },
    addMenu(id){
     this.dialogFormVisible=true
      this.form={}
        this.form.pid=id
      this.getIcon()
    },
    delbath(){
      let ids=this.multipleSelection.map(v =>v.id)
      request.post("menu/deletebatch",ids).then(res => {
        if (res.code == "200") {
          this.load()
          this.$message.success("删除客户成功")
        } else {
          this.$message.error("删除客户失败")
        }
      })
    },
    deletekehu(id) {
      console.log(id)
      request.post("menu/delete",id).then(res => {
        if (res.code == "200") {
          this.load()
          this.$message.success("删除客户成功")
        } else {
          this.$message.error("删除客户失败")
        }
      })
    },
    updatekehu(row){
      this.form=row
      this.dialogFormVisible=true
      this.getIcon()
    },
    saveKehu(){
      this.dialogFormVisible = false
      if (this.form.id){
        request.post("menu/update",this.form).then(res =>{
          if (res.code=="200"){
            this.load()
            this.$message.success("修改客户信息成功")
          }else {
            this.$message.error("修改客户信息失败")
          }
        })

      }else {
       if (this.form.pid==null){
         this.form.pid=0
       }
        request.post("menu",this.form).then(res =>{
          if (res.code=="200"){
            this.load()
            this.$message.success("新增菜单成功")
          }else {
            this.$message.error("新增菜单失败")
          }
        })
      }
    },
    openKehudialog(){
      this.form={}
      this.getIcon()
      this.dialogFormVisible=true``
    },
    resetForm() {
      this.name = ""
      this.load()
    },
    seachKehu() {
      this.load()
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    load() {
      request.get("menu/all", {
        params: {
          name: this.name
        }
      }).then(res => {
        if (res.code=="200"){
          this.tableData = res.data
        }

      })
    }
  }

}
</script>
<style scoped>

</style>